package Util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {

	public static Connection getConnection() throws Exception {
		//1.读取配置文件中的4个基本信息
		InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

		Properties pros = new Properties();
		pros.load(is);

		int[] a = {1 ,2, 3};
		String user = pros.getProperty("user");
		String password = pros.getProperty("password");
		String url = pros.getProperty("url");
		String driverClass = pros.getProperty("driverClass");

		//加载驱动
		Class.forName(driverClass);

		//3.获取连接
		Connection conn = DriverManager.getConnection(url,user,password);
		return conn;
	}

	public static void closeResource(Connection conn, Statement ps){
		try {
			if (ps != null){
				ps.close();
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}

		try {
			if (conn != null){
				conn.close();
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}
}
